package com.ypj.dao.impl;

import com.ypj.dao.UserDao;
import com.ypj.domain.User;
import com.ypj.utils.Jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User getLoginUser(String userCode) {
        User user=null;
        Jdbc.getCon();
        String sql="select * from smbms_user where userCode=?";
        PreparedStatement ps=Jdbc.createStatement(sql);
        ResultSet rs=null;
        try {
            ps.setString(1,userCode);
            rs=ps.executeQuery();
            while (rs.next()){
                user=new User();
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(rs.getInt("userRole"));
                user.setCreatedBy(rs.getInt("createdBy"));
                user.setCreationDate(rs.getTimestamp("creationDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
                user.setModifyDate(rs.getTimestamp("modifyDate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close(rs);
            Jdbc.close();
        }
        return user;
    }

    @Override
    public int updatePwd(int id,String usePassword) {
        Jdbc.getCon();
        String sql="update smbms_user set userPassword=? where id=?";
        PreparedStatement ps=Jdbc.createStatement(sql);
        int num=0;
        try {
            ps.setString(1,usePassword);
            ps.setLong(2,id);
            num=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Jdbc.close();
        }
        return num;
    }

}
/*class  A{
    public static void main(String[] args) {
        UserDao userDao=new UserDaoImpl();
        int n=userDao.updatePwd(1,"12345678");
        System.out.println(n);
    }
}*/
